package org.academy.funfair;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;

public class SpringBootStartListener implements ApplicationListener<ApplicationStartedEvent> {

	private Logger logger = LoggerFactory.getLogger(getClass());

	@Override
	public void onApplicationEvent(ApplicationStartedEvent event) {
		setWorkHome();
	}

	private void setWorkHome() {
		final String appName = FunfairProperties.getName();
		final String workHome = System.getProperty(appName + ".workhome", System.getProperty("user.home") + "/." + appName);
		
		System.setProperty("spring.config.location", "file:" + workHome + "/config/");
		System.setProperty(appName + ".workhome", workHome);
		logger.debug("Funfair setWorkHome: [spring.config.location={}, {}.workhome={}]",
				System.getProperty("spring.config.location"),
				appName,
				System.getProperty(appName + ".workhome"));
	}

}
